package com.code.generator.cn.util.fb;

import com.code.generator.cn.entity.CoderField;
import com.code.generator.cn.entity.CoderTable;
import com.code.generator.cn.util.MyFileUtil;
import com.code.generator.cn.util.PathUtil;
import com.code.generator.cn.util.base.CommonUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FbControllerUtil {

    public static void createController(CoderTable coderTable, List<CoderField> coderFields, Configuration configuration, String projPath) throws IOException, TemplateException {
        Writer file =null;
        String className=coderTable.getModifyName();
        Template temp=configuration.getTemplate("/fb/controller.ftl");

        //获取包名
        String packageName  = PathUtil.getCreateBasePath(coderTable);

        String groupPath=packageName.replace(".","/");
        //生成entity的路径
        String mapperPath=projPath+"/src/main/java/"+groupPath+"/controller/";
        //设置生成的路径
        MyFileUtil.createFileDir(mapperPath);
        //设置生成的属性
        Map<String,Object> createMap=new HashMap<>();
        CommonUtil.createFbBaseInfo(coderTable, createMap);
        CommonUtil.createPrimary(coderFields, createMap);
        //设置生成的文件名
        file = new FileWriter(mapperPath+className+"Controller.java");
        temp.process(createMap, file);
        file.close();

    }
}
